package com.masterclient.app.businessmodule.homemodule.view.adapter.template;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.shoplist.ProductHomeMessage;
import com.masterclient.app.businessmodule.homemodule.view.adapter.recommand.HomeGoodsRecyclerAdapter;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.widget.StaggeredGridNoScrollLayoutManager;

import java.util.ArrayList;
import java.util.List;


public class HomePageFooterAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    private List<ProductHomeMessage> sh_rem_collection = new ArrayList<>();
    private RecyclerView hotRecyclerView;
    private HomeGoodsRecyclerAdapter goodsRecyclerAdapter;

    public HomePageFooterAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public HomePageFooterAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<ProductHomeMessage> sh_rem_collection) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.sh_rem_collection = sh_rem_collection;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        hotRecyclerView = holder.itemView.findViewById(R.id.recycler);
        StaggeredGridNoScrollLayoutManager layoutManager = new StaggeredGridNoScrollLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        hotRecyclerView.setLayoutManager(layoutManager);
        hotRecyclerView.setItemAnimator(null);
        if (hotRecyclerView.getItemDecorationCount() == 0) {
            hotRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    outRect.set(ScreenUtils.dp2px(4),0 , ScreenUtils.dp2px(4), ScreenUtils.dp2px(8));
                }
            });
            goodsRecyclerAdapter = new HomeGoodsRecyclerAdapter();
//            goodsRecyclerAdapter.setStrings("首页", title);
            hotRecyclerView.setAdapter(goodsRecyclerAdapter);
            goodsRecyclerAdapter.bindToRecyclerView(hotRecyclerView);
            goodsRecyclerAdapter.setNewData(sh_rem_collection);
            goodsRecyclerAdapter.setEnableLoadMore(true);
        }
    }


    public void addData(List<ProductHomeMessage>productMessageList) {
        goodsRecyclerAdapter.addData(productMessageList);
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 20;
    }
}
